package com.wandian.weather.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;


import com.wandian.weather.R;

import java.util.List;

/**
 * 天气分布图
 */

public class YtPieView extends View {

    // 圆环画笔
    private Paint mRingPaint;
    // 线段和文字画笔
    private Paint mLinePaint;
    // 实心圆画笔
    private Paint mCirclePaint;

    // 圆环半径
    private float mRingRadius;
    // 圆环宽度
    private float mRingWidth;
    // 实心园半径
    private float mCircleRadius = 5;
    // 字的高度
    private int mTextHeight;
    //默认文字大小
    private float mTextSize;
    // 线段长度
    private float mLineLength;

    // 圆心x坐标
    private int mXCenter;
    // 圆心y坐标
    private int mYCenter;

    private List<Data> mData;// 数据集合
    private float mSum;// 总数

    private float mHeight;
    private float mTextMargin = 20;// 底部文字说明的边距
    private float mRectWdith;// 底部方块边长
    private float mScreenWdith;
    private Context context;
    private Bitmap bitmapFall;

    public YtPieView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        mScreenWdith = getScreenWidth(context);
        // 圆环大小默认为屏幕的六分之一宽度
//		mRingRadius = typeArray.getDimension(R.styleable.YtPieView_radius, mScreenWdith / 4);
        mRingRadius = sp2px(context, 80f);
        //默认为半径的三分之一
//		mRingWidth = typeArray.getDimension(R.styleable.YtPieView_ringWidth, mRingRadius / 5);
        mRingWidth = sp2px(context, 10f);
        //默认线长为圆环宽度
        mLineLength = mRingRadius /4+10;
        //文字大小
        mTextSize = sp2px(context, 10);
        //方块边长默认为文字大小
        mRectWdith = mTextSize;

        //释放资源

        // 圆环画笔
        mRingPaint = new Paint();
        mRingPaint.setAntiAlias(true);
        mRingPaint.setStyle(Paint.Style.STROKE);
        mRingPaint.setStrokeWidth(mRingWidth);

        // 线段和文字画笔
        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setStrokeWidth(0);
        mLinePaint.setTextSize(sp2px(context, 14));
        mLinePaint.setColor(Color.parseColor("#FFFFFF"));

        // 实心圆画笔
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setStyle(Paint.Style.FILL);
        mCirclePaint.setStrokeWidth(mCircleRadius);

        Paint.FontMetrics fm = mLinePaint.getFontMetrics();
        mTextHeight = (int) Math.ceil(fm.descent - fm.ascent);

        bitmapFall = BitmapFactory.decodeResource(context.getResources(), R.drawable.img_weather_point);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mHeight = 2 * (mRingRadius + mLineLength + mTextHeight);
        // super.onDraw(canvas);
        if (mSum == 0) {
            // 只画一个灰色的圆形
            return;
        }
        if (mData != null) {
            mXCenter = getWidth() / 2;
            mYCenter = (int) (mRingRadius + mLineLength + mTextHeight);

            RectF oval = new RectF();
            oval.left = (mXCenter - mRingRadius);
            oval.top = (mYCenter - mRingRadius);
            oval.right = mRingRadius * 2 + (mXCenter - mRingRadius);
            oval.bottom = mRingRadius * 2 + (mYCenter - mRingRadius);
            float start = -90f;
            for (Data data : mData) {
                mRingPaint.setColor(data.color);
                float ring = (data.value / mSum) * 360f;
                canvas.drawArc(oval, start, ring, false, mRingPaint);

                Paint p = new Paint();
                p.setAntiAlias(true);
                p.setColor(Color.parseColor("#FFFFFF"));
                p.setTextSize(sp2px(context, 16f));
                String str = "主要天气";
                Paint.FontMetrics fm = p.getFontMetrics();
                int height1 = (int) Math.ceil(fm.descent - fm.ascent);


                Paint p2 = new Paint();
                p2.setAntiAlias(true);
                p2.setColor(Color.parseColor("#FFFFFF"));
                p2.setTextSize(sp2px(context, 14f));
                String str2 = "天气分布情况";

                int margin = height1 / 2;
                canvas.drawText(str, mXCenter - p.measureText(str) / 2, mYCenter - margin, p);
                canvas.drawText(str2, mXCenter - p2.measureText(str2) / 2, mYCenter - margin + height1 + 6, p2);

                // 画线段
                Path path = new Path();
//                mLinePaint.setColor(data.color);
                // 画第一个线段
                float x = mXCenter + (float) Math.sin(Math.PI / 180.0 * (start + 90.0 + ring / 2.0)) * (mRingRadius + bitmapFall.getWidth() + 10);
                float y = mYCenter - (float) Math.cos(Math.PI / 180.0 * (start + 90.0 + ring / 2.0)) * (mRingRadius + bitmapFall.getHeight() + 10);

                canvas.drawBitmap(bitmapFall, x - bitmapFall.getWidth() / 2, y - bitmapFall.getHeight() / 2, new Paint());
                path.moveTo(x, y);

                float x2 = mXCenter + (float) Math.sin(Math.PI / 180.0 * (start + 90.0 + ring / 2.0)) * (mRingRadius + mLineLength);
                float y2 = mYCenter - (float) Math.cos(Math.PI / 180.0 * (start + 90.0 + ring / 2.0)) * (mRingRadius + mLineLength);

                path.lineTo(x2, y2);

                Bitmap bm = BitmapFactory.decodeResource(context.getResources(), data.resId);
                // 画第二条线段
                if (x2 >= mXCenter) {
                    float x3 = x2 + mLineLength * 2 + bm.getWidth();
                    path.lineTo(x3, y2);
                } else {
                    float x3 = x2 - mLineLength * 2 - bm.getWidth();
                    path.lineTo(x3, y2);
                }
                canvas.drawPath(path, mLinePaint);

                start += ring;
                // 画文字
                if (x2 >= mXCenter) {
                    canvas.drawBitmap(bm, x2 + 10, y2 - bm.getHeight() - 5, null);
                    canvas.drawText(data.title, x2 + bm.getWidth() + 10, y2 - 15, mLinePaint);
                } else {
                    float x3 = x2 - mLineLength * 2 - bm.getWidth();
                    canvas.drawBitmap(bm, x3 + 10, y2 - bm.getHeight() - 5, null);
                    canvas.drawText(data.title, x3 + 10 + bm.getWidth(),
                            y2 - 15, mLinePaint);
                }
            }

        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        widthMeasureSpec = MeasureSpec.makeMeasureSpec((int) mScreenWdith, MeasureSpec.AT_MOST);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) mHeight, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void setData(List<Data> datas) {
        this.mData = datas;
        mSum = 0;
        for (Data data : datas) {
            mSum += data.value;
        }

        mHeight = 2 * (mRingRadius + mLineLength + mTextHeight);
        mHeight += ((datas.size() / 2 + 1 + (datas.size() % 2 == 0 ? 0 : 1)) * (3 * mRectWdith));
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = (int) mHeight;
        setLayoutParams(params);

        invalidate();
    }

    private int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    private float sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (spValue * fontScale + 0.5f);
    }

    public static class Data {
        String title;// 标题
        int value;// 值
        int color;// 颜色
        int resId;

        public Data() {

        }

        public Data(String title, int value, int color, int resId) {
            this.title = title;
            this.value = value;
            this.color = color;
            this.resId = resId;
        }
    }
}
